package com.example.mapinformationandroid.UI.Fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.util.ArrayMap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.mapinformationandroid.ConstantData.Define;
import com.example.mapinformationandroid.Http.HTTPBase;
import com.example.mapinformationandroid.Model.ResponseBase;
import com.example.mapinformationandroid.Model.User;
import com.example.mapinformationandroid.R;
import com.example.mapinformationandroid.UI.Home;
import com.example.mapinformationandroid.UI.Login;

import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

import static android.content.Context.MODE_PRIVATE;


public class LoginFragment extends Fragment implements View.OnClickListener {
    private Button btn_login;
    private EditText et_phone,et_password;
    private HTTPBase httpBase;
    private User user;
    private Map<String,String> login_map;
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;
    Context mContext;


    @Override
    public void onStart() {
        super.onStart();
        mContext=getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_login, container, false);
    }



    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        initData();

    }

    protected void initView(View view){
        btn_login = view.findViewById(R.id.btn_login);
        btn_login.setOnClickListener(this);
        et_phone =view.findViewById(R.id.login_et_phone);
        et_password = view.findViewById(R.id.login_et_passwords);

    }
    protected void initData(){
        httpBase = new HTTPBase();
        user = new User();
        login_map = new ArrayMap<>();
        sharedPreferences = getContext().getSharedPreferences("user",MODE_PRIVATE);
        editor = sharedPreferences.edit();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_login:
                if(et_password.getText()!= null&& et_phone != null) {
                    login_map.put("password",et_password.getText().toString());
                    login_map.put("phone",et_phone.getText().toString());
                    httpBase.login(login_map, new Observer<ResponseBase<User>>() {
                        @Override
                        public void onSubscribe(Disposable d) {

                        }

                        @Override
                        public void onNext(ResponseBase<User> value) {
                            if(value.getCode() == httpBase.SUCCESS){
                                Log.d("login","Success");
                                user = value.getData();
                                setUserData(user);
                                Toast.makeText(getContext(),"登陆成功",Toast.LENGTH_LONG).show();
                                startActivity(new Intent(getContext(), Home.class));

                            }
                        }

                        @Override
                        public void onError(Throwable e) {
                            Log.d("login","Error",e);
                        }

                        @Override
                        public void onComplete() {
                            Log.d("login","Complete");

                            getActivity().finish();
                        }
                    });
                }else{

                    Toast.makeText(getContext(),"请登录账号或密码",Toast.LENGTH_LONG).show();
                }

        }

    }
    public void setUserData(User user){
        editor.putInt(Define.UserDefine.USER_ID,user.getId());
        editor.putString(Define.UserDefine.USER_NAME,user.getName());
        editor.putString(Define.UserDefine.USER_PHONE,user.getPhone());
        editor.putInt(Define.UserDefine.USER_EXAMINE,user.getExamine());
        editor.putString(Define.UserDefine.USER_TOKEN,user.getToken());
        editor.putString(Define.UserDefine.USER_HEAD_PICTURE,user.getHead_picture());
        editor.putInt(Define.UserDefine.USER_SUPPORT,user.getSupport());
        editor.commit();
    }
}